package com.xiaobai.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.net.InetAddress;
import java.util.Scanner;

public class ChatRoomClient1 {
    private int serverPort;
    private String serverHost;

    public ChatRoomClient1(String serverHost, int serverPort) {
        this.serverPort = serverPort;
        this.serverHost = serverHost;
    }

    public static void main(String[] args) throws InterruptedException {
        new ChatRoomClient1("localhost", 1234).run();
    }

    public void run() throws InterruptedException {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup).channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();

                            // On top of the SSL handler, add the text line codec.
                            pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new StringEncoder());

                            // and then business logic.
                            pipeline.addLast(new ChatClientHandler());
                        }
                    });
            Channel channel = bootstrap.connect(serverHost, serverPort).sync().channel();
            Scanner scanner = new Scanner(System.in);
            while (true) {
                String text = scanner.nextLine();
                channel.writeAndFlush(text + "\r\n");
                if ("bye".equals(text)) {
                    channel.closeFuture().sync();
                }

            }

        } finally {
            workerGroup.shutdownGracefully();
        }

    }

    public class ChatClientHandler extends SimpleChannelInboundHandler<String> {
        private final ChannelGroup CHANNELS = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
            System.err.println("receive:" + msg);
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            CHANNELS.add(ctx.channel());
        }
    }

}

